<template>
	<div>
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="68px">
			<el-form-item label="字典名称" prop="dicTypeName">
				<el-input v-model="queryParams.dicTypeName" placeholder="请输入字典名称" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="字典类型" prop="dicType">
				<el-input v-model="queryParams.dicType" placeholder="请输入字典类型" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="状态" prop="dicTypeState">
				<el-select v-model="queryParams.dicTypeState" placeholder="字典状态" clearable>
					<el-option v-for="dict in dict" :key="dict.value" :label="dict.label" :value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item label="创建时间" prop="createTime">
				<el-date-picker value-format="yyyy-MM-dd HH:mm:ss" style="width: 240px;" v-model="valuetime"
					type="datetimerange" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['12:00:00']">
				</el-date-picker>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="quarryForm()">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="reset()">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="addFrom()">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single"
					@click="updateData()">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
					@click="deleteData()">删除</el-button>
			</el-col>
		</el-row>

		<el-table ref="multipleTable" :data="dataList" style="width: 100%" @selection-change="handleSelectionChange"
			v-loading="loading">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column prop="dicTypeId" label="字典编号" align="center">
			</el-table-column>
			<el-table-column prop="dicTypeName" label="字典名称" align="center">
			</el-table-column>
			<el-table-column prop="dicType" label="字典类型" align="center" show-overflow-tooltip>
				<template slot-scope="scope">
					<el-link @click="pushType(scope.row.dicType)" style="margin-right:10px;color: #0099CC;"
						target="_blank">
						{{scope.row.dicType}}
					</el-link>
				</template>
			</el-table-column>
			<el-table-column prop="dicTypeState" label="状态" align="center">
				<template v-slot="scope">
					<div v-if="scope.row.dicTypeState == 0">
						<el-tag>正常</el-tag>
					</div>
					<div v-if="scope.row.dicTypeState == 1">
						<el-tag type="info">停用</el-tag>
					</div>
				</template>
			</el-table-column>
			<el-table-column prop="remark" label="备注" align="center" show-overflow-tooltip>
			</el-table-column>
			<el-table-column label="创建日期" align="center">
				<template slot-scope="scope">{{ scope.row.createTime }}</template>
			</el-table-column>

			<el-table-column label="操作" align="center">
				<template slot-scope="scope">
					<el-button size="mini" @click="handleEdit(scope.row)">修改
					</el-button>
					<el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除
					</el-button>
				</template>
			</el-table-column>
		</el-table>

		<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
			:current-page="queryParams.pageNum" :page-sizes="[3, 5, 8, 10]" :page-size="queryParams.pageSize"
			layout="->,total, sizes, prev, pager, next, jumper" :total="queryParams.total">
		</el-pagination>

		<!-- 添加或修改参数配置对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="字典名称" prop="dicTypeName">
					<el-input v-model="form.dicTypeName" placeholder="请输入字典名称" />
				</el-form-item>
				<el-form-item label="字典类型" prop="dicType">
					<el-input v-model="form.dicType" placeholder="请输入字典类型" />
				</el-form-item>
				<el-form-item label="状态" prop="dicTypeState">
					<el-radio-group v-model="form.dicTypeState">
						<el-radio v-for="dict in dict" :key="dict.value" :label="dict.value">{{dict.label}}</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="备注" prop="remark">
					<el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				//查询属性
				queryParams: {
					dicTypeName: '',
					dicType: '',
					dicTypeState: '',
					beginTime: '',
					endTime: '',
					pageNum: 1,
					pageSize: 5,
					total: 10,
				},
				//新增,修改属性
				form: {
					dicTypeId: '',
					dicTypeName: '',
					dicType: '',
					dicTypeState: 0,
					remark: '',
				},
				//时间选择器
				valuetime: [

				],
				chooseAll: [

				],
				//状态值
				dict: [{
						"label": "正常",
						"value": 0
					},
					{
						"label": "停用",
						"value": 1
					}
				],
				rules: {
					dicTypeName: [{
						required: true,
						message: "字典名称不能为空",
						trigger: "blur"
					}],
					dicType: [{
						required: true,
						message: "字典类型不能为空",
						trigger: "blur"
					}]
				},
				//列表数据集
				dataList: [],
				showSearch: true,
				multiple: true,
				single: true,
				open: false,
				loading: false,
				title: '',
			};
		},
		methods: {
			//s设置表头行的样式
			tableHeaderColor({
				row,
				column,
				rowIndex,
				columnIndex
			}) {
				return 'background-color:#dcdcdc;color:#6b6b6b;font-wight:100;font-size:15px;text-align:center'

			},
			//链接跳转
			pushType(val) {
				this.$router.push({
					path: '/data',
					query: {
						dicType: val,
					}
				});
			},
			//表单取消
			cancel() {
				this.open = false;
				this.$refs["form"].resetFields();
			},
			//表单确定函数
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						this.open = false;
						if (this.form.dicTypeId == null || this.form.dicTypeId == '' || this.form.dicTypeId ==
							undefined) {
							this.$axios.post("/type", this.form).then(resp => {
								if (resp.code == 20000) {
									this.$message.success(resp.msg);
									this.quarryAll(this.queryParams);
								} else {
									this.$message.error(resp.msg);
								}
							})
						} else {
							this.$axios.put("/type", this.form).then(resp => {
								if (resp.code == 20000) {
									this.$message.success(resp.msg);
									this.quarryAll(this.queryParams);
								} else {
									this.$message.error(resp.msg);
								}
							})
						}
					}
				});
			},
			//表单多选框改变函数
			handleSelectionChange(val) {
				this.chooseAll = val;
				console.log(this.chooseAll);
				if (val.length == 1) {
					this.single = false;
				} else {
					this.single = true;
				}
				if (val.length >= 1) {
					this.multiple = false;
				} else if (val.length == 0) {
					this.multiple = true;
				}

			},
			//单页Size改变
			handleSizeChange(val) {
				this.queryParams.pageSize = val;
				this.quarryAll(this.queryParams);
			},
			//页码改变
			handleCurrentChange(val) {
				this.queryParams.pageNum = val;
				this.quarryAll(this.queryParams);
			},
			//表单删除函数
			handleDelete(row) {
				let id = row.dicTypeId;
				this.delByIds(id);
			},
			//表单修改函数
			handleEdit(row) {
				this.title = "修改字典类型";
				console.log(row);
				this.form = row;
				this.open = true;
			},
			//删除函数(可躲删)
			deleteData() {
				let arr = this.chooseAll;
				let idArr = [];
				let i = 0;
				//拿到所有选中的数据从中获取所有id
				while (i < arr.length) {
					idArr.push(arr[i].dicTypeId);
					i++;
				}
				let ids = idArr.toString();
				/* console.log(ids); */
				this.delByIds(ids);
			},
			//修改函数
			updateData() {
				this.title = "修改字典类型";
				console.log(this.chooseAll);
				this.form = this.chooseAll[0];
				this.open = true;
			},
			//新增函数
			addFrom() {
				this.title = "添加字典类型";
				this.open = true;
				this.form = {
					dicTypeId: '',
					dicTypeName: '',
					dicType: '',
					dicTypeState: 0,
					remark: '',
				};
			},
			reset() {
				this.queryParams = {
						dicTypeName: '',
						dicType: '',
						dicTypeState: '',
						beginTime: '',
						endTime: '',
						pageNum: 1,
						pageSize: 5,
						total: 10,
					},
					this.valuetime = [];
				this.quarryAll(this.queryParams);
			},
			//查询函数
			quarryForm() {
				console.log(this.valuetime);
				this.queryParams.beginTime = this.valuetime[0];
				this.queryParams.endTime = this.valuetime[1];
				this.quarryAll(this.queryParams);

			},
			quarryAll(params) {
				this.loading = true;
				this.$axios.get("/type", {
					params: params
				}).then(resp => {
					if (resp.code == 20000) {
						console.log(resp);
						this.dataList = resp.data.list;
						this.queryParams.pageNum = resp.data.pageNum;
						this.queryParams.pageSize = resp.data.pageSize;
						this.queryParams.total = resp.data.total;
					}
				})
				this.loading = false;
			},
			//通过ids删除(一或多都可以)
			delByIds(ids) {
				this.$confirm(`此操作将永久删除编号为${ids}的数据, 是否继续?`, '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.$axios.delete("/type?ids=" + ids).then(resp => {
						if (resp.code == 20000) {
							this.$message.success(`删除编号为${ids}的数据` + resp.msg);
							this.quarryAll(this.queryParams);
						} else {
							this.$message.error(`删除编号为${ids}的数据` + resp.msg);
						}
					});
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});
			}
		},
		mounted() {
			this.quarryAll('');
		}
	};
</script>

<style>
</style>
